package cn.wang.service.sysadmin.Impl;

import cn.wang.dao.BaseDao;
import cn.wang.entity.sysadmin.Module;
import cn.wang.entity.sysadmin.Role;
import cn.wang.entity.sysadmin.User;
import cn.wang.service.sysadmin.RoleService;
import cn.wang.service.sysadmin.UserService;
import cn.wang.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private BaseDao<Role,String>baseDao;
    @Autowired
    private BaseDao<Module,String>moduleStringBaseDao;

    @Override
    public Pagination findPage(int pageNo, int pageSize) {
        return baseDao.pageByHql("from Role",pageNo,pageSize);
    }

    @Override
    public List<Role> findAll() {
        return baseDao.getListByHQL("from Role");
    }

    @Override
    public Role findById(String id) {
        return baseDao.get(Role.class,id);
    }

    @Override
    public void save(Role model) {
        //一对一相互关联
        //model.getRoleInfo().setRole(model);

        baseDao.save(model);
    }

    @Override
    public void delete(String[] ids) {
        for (String id :ids) {
            baseDao.deleteById(Role.class,id);
        }
    }

    @Override
    public void update(Role model) {
        //查询出老数据
        Role oldRole=baseDao.get(Role.class,model.getId());
        oldRole.setName(model.getName());
        oldRole.setRemark(model.getRemark());
        baseDao.update(oldRole);
    }

    @Override
    public void updateModule(String id, String[] moduleids) {
        Role role=baseDao.get(Role.class,id);
        Set<Module>moduleSet =new HashSet<>(moduleids.length);
        for (int i=0;i<moduleids.length;i++){
            System.out.println(moduleids[i]);
           Module module= moduleStringBaseDao.get(Module.class,moduleids[i]);
            System.out.println(module);
            moduleSet.add(module);
        }
        role.setModuleSet(moduleSet);
        baseDao.update(role);
    }
}
